package com.wpen.unit.service.job.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.wpen.unit.service.job.BatchExecLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wpen.unit.cvt.job.BatchExecLogCvt;
import com.wpen.unit.cvt.job.BatchExecRedologCvt;
import com.wpen.unit.enums.PlanType;
import com.wpen.unit.enums.YN;
import com.wpen.unit.exception.BizException;
import com.wpen.unit.infra.mybatis.job.po.BatchExecLogPo;
import com.wpen.unit.infra.mybatis.job.po.BatchExecRedologPo;
import com.wpen.unit.infra.mybatis.job.po.BatchInfoPo;
import com.wpen.unit.infra.mybatis.job.po.BatchPlanPo;
import com.wpen.unit.infra.repository.job.BatchExecLogRepository;
import com.wpen.unit.infra.repository.job.BatchExecRedologRepository;
import com.wpen.unit.infra.repository.job.BatchInfoRepository;
import com.wpen.unit.infra.repository.job.BatchPlanRepository;
import com.wpen.unit.utils.DateUtil;
import com.wpen.unit.utils.IdUtil;
import com.wpen.unit.web.dto.job.BatchExecLogListRequest;
import com.wpen.unit.web.dto.job.BatchExecLogListResponse;
import com.wpen.unit.web.dto.job.BatchExecLogListRowsResponse;
import com.wpen.unit.web.dto.job.BatchExecRedoRequest;
import com.wpen.unit.web.dto.job.BatchExecRedoResponse;
import com.wpen.unit.web.dto.job.BatchExecRedologListRequest;
import com.wpen.unit.web.dto.job.BatchExecRedologListResponse;
import com.wpen.unit.web.dto.job.BatchExecRedologListRowsResponse;
import com.wpen.unit.web.dto.job.BatchInfoMtRunningRequest;
import com.wpen.unit.web.dto.job.BatchInfoMtRunningResponse;
import com.wpen.unit.web.dto.job.StatRequest;
import com.wpen.unit.web.dto.job.StatResponse;

@Service
public class BatchExecLogServiceImpl implements BatchExecLogService {
	@Autowired
	private BatchInfoRepository batchInfoRepository;
	@Autowired
	private BatchPlanRepository batchPlanRepository;
	@Autowired
	private BatchExecLogRepository batchExecLogRepository;
	@Autowired
	private BatchExecRedologRepository batchExecRedologRepository;

	@Override
	public BatchInfoMtRunningResponse mtRunBatchInfo(BatchInfoMtRunningRequest batchInfoMtRunningRequest) {
		String bizCode = batchInfoMtRunningRequest.getBizCode();
		String batchCode = batchInfoMtRunningRequest.getBatchCode();
		BatchInfoPo batchInfoPo = batchInfoRepository.queryByCode(bizCode, batchCode);
		if (batchInfoPo == null) {
			throw new BizException("E00001", bizCode + "-" + batchCode);
		}
		// 批次日期，，采用自然日
		BatchPlanPo batchPlanPo = new BatchPlanPo();
		batchPlanPo.setId(IdUtil.nextId());
		batchPlanPo.setBizCode(batchInfoPo.getBizCode());
		batchPlanPo.setBatchCode(batchInfoPo.getBatchCode());
		// 手工类型
		batchPlanPo.setPlanType(PlanType.Manual.value());
		batchPlanPo.setEnabled(YN.Yes.value());
		batchPlanPo.setTriggerLastTime(0L);
		batchPlanPo.setTriggerNextTime(System.currentTimeMillis());
		batchPlanPo.setCreateTime(new Date());
		batchPlanRepository.insert(batchPlanPo);

		BatchInfoMtRunningResponse mtRunningResponse = new BatchInfoMtRunningResponse();
		return mtRunningResponse;
	}

	@Override
	public StatResponse statTodayBatchExec(StatRequest statRequest) {
		StatResponse statResponse = new StatResponse();
		// 数据库查询
		BatchExecLogPo batchExecLogPo = new BatchExecLogPo();
		batchExecLogPo.setExecDate(DateUtil.format(DateUtil.FORMAT_DATE2));
		long total = batchExecLogRepository.count(batchExecLogPo);
		statResponse.setCount(total);
		batchExecLogPo.setExecDate(DateUtil.offset(Calendar.DAY_OF_YEAR, -1, DateUtil.FORMAT_DATE2));
		long total2 = batchExecLogRepository.count(batchExecLogPo);
		statResponse.setAlive(total2);
		return statResponse;
	}

	@Override
	public BatchExecLogListResponse queryListBatchExecLog(BatchExecLogListRequest batchExecLogListRequest) {
		int index = batchExecLogListRequest.getIndex();
		int size = batchExecLogListRequest.getSize();
		// 数据库查询
		BatchExecLogPo batchExecLogPo = BatchExecLogCvt.INSTANCE.queryListDto2Do(batchExecLogListRequest);

		long total = batchExecLogRepository.count(batchExecLogPo);
		List<BatchExecLogPo> batchExecLogPoLists = batchExecLogRepository.queryList(index, size, batchExecLogPo);

		// 返回响应信息
		BatchExecLogListResponse batchExecLogListResponse = new BatchExecLogListResponse();
		batchExecLogListResponse.setTotal(total);
		batchExecLogListResponse.setCount(0);
		if (batchExecLogPoLists != null) {
			batchExecLogListResponse.setCount(batchExecLogPoLists.size());
			List<BatchExecLogListRowsResponse> listRows = batchExecLogListResponse.getRows();
			for (BatchExecLogPo inBatchExecLogPo : batchExecLogPoLists) {
				BatchExecLogListRowsResponse row = BatchExecLogCvt.INSTANCE.queryListRowDo2Dto(inBatchExecLogPo);
				row.setExecTime(DateUtil.execTime(inBatchExecLogPo.getStartupTime(), inBatchExecLogPo.getEndTime()));
				listRows.add(row);
			}
			batchExecLogListResponse.setRows(listRows);
		}

		return batchExecLogListResponse;
	}

	@Override
	public BatchExecRedologListResponse queryListBatchExecRedolog(
			BatchExecRedologListRequest batchExecRedologListRequest) {
		int index = batchExecRedologListRequest.getIndex();
		int size = batchExecRedologListRequest.getSize();
		// 数据库查询
		BatchExecRedologPo batchExecRedologPo = BatchExecRedologCvt.INSTANCE
				.queryListDto2Do(batchExecRedologListRequest);

		List<BatchExecRedologPo> batchExecRedologPoLists = batchExecRedologRepository.queryList(index, size,
				batchExecRedologPo);

		// 返回响应信息
		BatchExecRedologListResponse batchExecRedologListResponse = new BatchExecRedologListResponse();
		batchExecRedologListResponse.setTotal(0);
		batchExecRedologListResponse.setCount(0);
		if (batchExecRedologPoLists != null) {
			batchExecRedologListResponse.setTotal(batchExecRedologPoLists.size());
			batchExecRedologListResponse.setCount(batchExecRedologPoLists.size());
			List<BatchExecRedologListRowsResponse> listRows = batchExecRedologListResponse.getRows();
			for (BatchExecRedologPo inBatchExecRedologPo : batchExecRedologPoLists) {
				BatchExecRedologListRowsResponse row = BatchExecRedologCvt.INSTANCE
						.queryListRowDo2Dto(inBatchExecRedologPo);
				listRows.add(row);
			}
			batchExecRedologListResponse.setRows(listRows);
		}

		return batchExecRedologListResponse;
	}

	@Override
	public BatchExecRedoResponse redoBatchExec(BatchExecRedoRequest batchExecRedologModRequest) {
		// 持久化操作
		BatchExecRedologPo batchExecRedologPo = BatchExecRedologCvt.INSTANCE.modDto2Do(batchExecRedologModRequest);
		// 查询数据是否存在
		BatchExecRedologPo batchExecRedologPoDb = batchExecRedologRepository.queryById(batchExecRedologPo.getId());
		if (batchExecRedologPoDb == null) {
			throw new BizException("E00001", batchExecRedologModRequest.getBatchLogId() + "");
		}
		// 更新数据 TODO
//        batchExecRedologRepository.updateById(batchExecRedologPo);

		// 返回响应信息
		BatchExecRedoResponse batchExecRedologModResponse = new BatchExecRedoResponse();
		return batchExecRedologModResponse;
	}

}
